import Http from '../utils/Http.js';
//获取本地存储的数据
let userinfo = JSON.parse(localStorage.getItem('userinfo')) || {};

//查询字符串转对象
function fsToObj(fs) {
    //获取？后面的查询字符串
    let url = fs.substr(1).split('&');
    //把查询字符串变成
    let obj = {};
    url.forEach(item => {
        obj[item.split('=')[0]] = decodeURIComponent(item.split('=')[1]);
    })
    return obj
}

//展示登录状态
function showLoginAndRegister() {
    $('.out').click(function () {
        if (confirm('您确认需要退出登录吗？')) {
            localStorage.removeItem('userinfo')
            location.href = '/'
        }
    })
    if (localStorage.getItem('userinfo')) {
        $('.user').css('display', 'inline-block');
        $('.login').css('display', 'none');
        showUser()
    } else {
        $('.login').css('display', 'inline-block');
        $('.user').css('display', 'none');
    }
}
// showLoginAndRegister()

//先给一个全局变量 async里面修改它的赋值
// let Allusername = ''
// let topHeadPhoto = ''
async function showUser() {
    let {
        result
    } = await Http({
        url: '/api/user',
        data: {
            mid: userinfo.mid
        }
    })
    // console.log(result);
    if (result.msg == '查无此人') {
        alert('登录验证失败,请重新登录');
        return;
    }
    // Allusername = result.result.username
    // topHeadPhoto = result.result.head_photo_url
    $('.user img').attr('src', result.result.head_photo_url)
    $('#ImUsername').html('我是' + result.result.username)
}

function getSearch() {
    //点击搜索跳转到搜索页面
    $('#searchBtn').on('click', function () {
        if ($('#keyword').val() == '') {
            alert('输入不能为空')
            return;
        }
        location.href = '../pages/search_list.html?keyword=' + $('#keyword').val()
    })
}

export {
    userinfo,
    fsToObj,
    showLoginAndRegister,
    // showUser,
    getSearch
};